const db = require('../config/db');

class User {
  static create(username, password, callback) {
    db.run('INSERT INTO users (username, password) VALUES (?, ?)', [username, password], callback);
  }

  static findByUsername(username, callback) {
    db.get('SELECT * FROM users WHERE username = ?', [username], callback);
  }

  static findById(id, callback) {
    db.get('SELECT * FROM users WHERE id = ?', [id], callback);
  }

  static updateStatus(userId, status, callback) {
    db.run(
      'UPDATE users SET status = ? WHERE id = ?',
      [status, userId],
      callback
    );
  }

  static updateLastActive(userId, callback) {
    const sql = 'UPDATE users SET last_active = CURRENT_TIMESTAMP WHERE id = ?';
    db.run(sql, [userId], callback);
  }

  static getStatus(userId, callback) {
    db.get(
      'SELECT status FROM users WHERE id = ?',
      [userId],
      callback
    );
  }

  static checkInactiveUsers(callback) {
    const sql = `
      UPDATE users
      SET status = 'offline'
      WHERE status = 'online'
      AND last_active < datetime('now', '-5 minutes')`;
    db.run(sql, callback);
  }
}

module.exports = User; 